//
//  SettingViewController.m
//  sign Detector
//
//  Created by Thanh Hai on 4/3/13.
//  Copyright (c) 2013 Thoi Thai. All rights reserved.
//

#import "SettingViewController.h"
#import "RecordingViewController.h"
#import "DataCenter.h"

#define ActionSheet_Require_Set_Sign    101
#define ActionSheet_Request_Update_Sign    102

static SettingViewController *_Share = nil;

@interface SettingViewController () <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIAlertViewDelegate>{
    IBOutlet UIImageView *_imageView;
}

- (IBAction)changeButtonTouchUpInside:(id)sender;

@end

@implementation SettingViewController

+ (SettingViewController*)shared{
    @synchronized([SettingViewController class]){
        if (!_Share) {
            _Share = [[SettingViewController alloc] init];
        }
        return _Share;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - Private Methods

- (IBAction)changeButtonTouchUpInside:(id)sender{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From Cameraroll", @"Take New Picture", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.tag = ActionSheet_Request_Update_Sign;
    [actionSheet showInView:self.navigationController.view];
}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.navigationController presentModalViewController:picker animated:YES];
    }else if (buttonIndex == 1){
#if TARGET_IPHONE_SIMULATOR
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Simulator cannot use the camera" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Close", nil];
        [alert show];
#else
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerCameraDeviceFront;
        picker.delegate = self;
        [self.navigationController presentViewController:picker animated:YES completion:^{
            
        }];
#endif
    }
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if ([[DataCenter shared] standardImageSign]) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From Cameraroll", @"Take New Picture", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
        actionSheet.tag = ActionSheet_Request_Update_Sign;
        [actionSheet showInView:self.navigationController.view];
    }else{
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"From Cameraroll", @"Take New Picture", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
        actionSheet.tag = ActionSheet_Require_Set_Sign;
        [actionSheet showInView:self.navigationController.view];
    }
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self.navigationController dismissModalViewControllerAnimated:YES];
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if ([[DataCenter shared] setNewStandardSignImage:image]) {
        NSLog(@"Change Success");
        _imageView.image = image;
        [[DataCenter shared] lock:^{
            if ([self.navigationController.viewControllers containsObject:[RecordingViewController shared]]) {
                [self.navigationController popToViewController:[RecordingViewController shared] animated:YES];
            }else{
                [self.navigationController pushViewController:[RecordingViewController shared] animated:YES];
            }
        }];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Set Standard Sign Failed \n Please Try again" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    if (![[DataCenter shared] standardImageSign]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"You have to set a sign" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
    }
}

#pragma mark - View's Life Circle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Settings";    
    // Do any additional setup after loading the view from its nib.
    if (![[DataCenter shared] standardImageSign]) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"From Cameraroll", @"Take New Picture", nil];
        actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
        actionSheet.tag = ActionSheet_Require_Set_Sign;
        [actionSheet showInView:self.navigationController.view];
    }else{
        _imageView.image = [[DataCenter shared] standardImageSign];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![[DataCenter shared] standardImageSign]) {
        self.navigationItem.hidesBackButton = YES;
    }else{
        self.navigationItem.hidesBackButton = NO;
    }    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
